Mybatis框架(6) —— MyBatis主配置文件中的常用标签

简介

  • 介绍 MyBatis主配置文件 中常用的三个标签
    • properties标签:用于数据库连接的内外部配置。
    • typeAliases标签:用于对全类名设置别名。
    • package标签:用于指定类。

properties标签

  • 在之前MyBatis主配置文件的环境搭建中,我们将连接数据库的4个基本信息编写在MyBatis主配置文件中,这种方式属于内部配置
  • 我们也可以选择外部配置的方式,只要我们通过properties标签来引用外部配置文件信息即可。

内部配置

SqlMapConfig.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


<configuration>

<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<!-- 内部配置:连接数据库的4个基本信息 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>

外部配置

  • 外部配置的两种方式

    • resource=”jdbcConfig.properties”
    • url=”file:///C:\Users\water\IDEA-workspace\mybatis_CRUD\src\main\resources\jdbcConfig.properties”
  • URL 统一资源定位符(Uniform Resource Locator)

  • URI 统一资源标识符(Uniform Resource Locator)

    • 在某个应用中,表示一个资源位置的唯一标识。

jdbcConfig.properties

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ee42
jdbc.username=root
jdbc.password=1234

SqlMapConfig.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- MyBatis主配置文件 -->
<configuration>

<!-- 外部配置的两种方式:resource或url -->
<properties resource="jdbcConfig.properties"></properties>
<properties url="file:///C:\Users\water\IDEA-workspace\mybatis_CRUD\src\main\resources\jdbcConfig.properties"></properties>

<!-- ................. -->
<!-- 配置连接数据库的4个基本信息 -->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- ................. -->

</configuration>

typeAliases标签

  • 在之前MyBatis主配置文件的环境搭建中,我们在持久层接口的映射配置文件中的resultType属性中,每次都需要输入冗长的实体类全类名。我们可以通过 typeAliases标签 来取别名,从而避免输入冗长的实体类全类名问题
  • 别名不区分大小写。

    实体类全类名

UserDao.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 添加用户 -->
<select id="add" parameterType="cn.water.domain.User" ></select>

<!-- 更新用户 -->
<select id="update" parameterType="cn.water.domain.User"></select>

<!-- 删除用户 -->
<select id="delete" parameterType="INT" ></select>

<!-- 查询所有 -->
<select id="findAll" resultType="cn.water.domain.User"></select>

<!-- 查询单个用户 -->
<select id="findOne" parameterType="INT" resultType="cn.water.domain.User"></select>

起别名

SqlMapConfig.xml

1
2
3
4
5
6
7
8
9
10
<configuration>
<!-- ................. -->

<!-- 起别名 -->
<typeAliases>
<typeAlias type="cn.water.domain.User" alias="user"></typeAlias>
</typeAliases>

<!-- ................. -->
</configuration>

UserDao.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 添加用户 -->
<select id="add" parameterType="user" ></select>

<!-- 更新用户 -->
<select id="update" parameterType="user"></select>

<!-- 删除用户 -->
<select id="delete" parameterType="INT" ></select>

<!-- 查询所有 -->
<select id="findAll" resultType="user"></select>

<!-- 查询单个用户 -->
<select id="findOne" parameterType="INT" resultType="user"></select>

package标签

  • 使用 TypeAliases标签 为每个实体类起别名的确简化了我们的操作,但我们也需要为每一个实体类手动的其别名,仍然有简化的余地。为了是操作更加简便,我们可以使用Package标签来指定包,来自动加载包下的所有实体类,并以其类名作为别名,不区分大小写。另外,Mappers标签中有Package属性,用于指定包,来自动加载包下的所有映射文件。相当于代替了所有的Mapper标签。

mappers标签

mapper标签

1
2
3
4
5
6
<mappers>
<mapper resource="cn/water/dao/UserDao.xml"></mapper>
<mapper resource="cn/water/dao/AccountDao.xml"></mapper>
<mapper resource="cn/water/dao/StudentDao.xml"></mapper>
<mapper resource="cn/water/dao/EmployeeDao.xml"></mapper>
</mappers>

package标签

1
2
3
<mappers>
<package name="cn.water.dao"/>
</mappers>

typeAliases标签

mapper标签

1
2
3
4
5
6
<typeAliases>
<typeAlias type="cn.water.domain.User" alias="user"></typeAlias>
<typeAlias type="cn.water.domain.Account" alias="account"></typeAlias>
<typeAlias type="cn.water.domain.Student" alias="studnet"></typeAlias>
<typeAlias type="cn.water.domain.Employee" alias="employee"></typeAlias>
</typeAliases>

package标签

  • 别名默认为类名
1
2
3
<typeAliases>
<package name="cn.water.domain"/>
</typeAliases>
-------------本文结束-------------
Donate comment here